home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4019 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.5 KB  |  70 lines

  1. Path: news2.ios.com!usenet
  2. From: vlad@gramercy.ios.com (Vlastimil Adamovsky)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Template casting problem
  5. Date: Sat, 27 Jan 1996 08:14:11 GMT
  6. Organization: Internet Online Services
  7. Message-ID: <4ecmfm$as9@news2.ios.com>
  8. References: <41@site73.site73.ping.at>
  9. NNTP-Posting-Host: ppp-33.ts-7.hck.idt.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. mgeramb@site73.site73.ping.at (Michael Geramb) wrote:
  13.  
  14. >Here is a C++ problem that I never managed to solve.
  15.  
  16. >Imagine you create a template of a class :
  17.  
  18. >template <class T> class A
  19. >{
  20. >        ...
  21. >};
  22.  
  23. >This gives a family of classes (instances), say:
  24.  
  25. >A<short>, A<int>, A<float> etc, which may coexist in one function.
  26. >Then, a problem arises how to cast one instance to another.
  27.  
  28. If you like to complicate things, so maybe this can be part of your
  29. solution:
  30.  
  31.  
  32.  
  33. template <class T> class A
  34. {
  35.   T x;
  36.   public:
  37.   A(T i) : x(i) {}
  38.   operator float() {return (float) x;}
  39.   operator int() {return (int) x;}
  40.   operator short() {return (short) x;}
  41.   operator double() {return (double) x;}
  42.   A(A<int> & i): x((T)i) {}
  43. };
  44.  
  45.  
  46. //example 
  47.  
  48. void main()
  49. {
  50.   A<int> a(6);
  51.   A<float> x = a;
  52.   A<short> i = x;
  53.   A<double> z = i;
  54. };
  55.  
  56.  
  57. >As a more concrete example, one may imagine a template CVector which
  58. >generates short, integer, float and double instances. 
  59.  
  60. What do you mean by that CVector generates short,integer,float etc?
  61. Here I got lost.
  62.  
  63.  
  64.  
  65. *******************************************
  66. *    Vlastimil Adamovsky                  *
  67. * Smalltalk, C++ and Envelop development  *
  68. *******************************************
  69.  
  70.